package deng.jbe.client;

import java.util.Properties;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.MessageProducer;
import javax.jms.Queue;
import javax.jms.Session;
import javax.naming.Context;
import javax.naming.InitialContext;

import org.apache.log4j.Logger;

public class SendMessageToQueue {


	public static void main(String[] args) {
		new SendMessageToQueue().run();
	}
	
	private static Logger logger = Logger.getLogger(SendMessageToQueue.class);

	public void run() {

		Properties props = new Properties();
		props.setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
		props.setProperty(Context.PROVIDER_URL, "localhost:1099");
		
		try {
			logger.info("Looking up ConnectionFactory");
			Context ctx = new InitialContext(props);
			ConnectionFactory connFac = (ConnectionFactory)ctx.lookup("ConnectionFactory");
			Queue exampleQueue = (Queue)ctx.lookup("/queue/ExampleQueue");
			Connection conn = connFac.createConnection();			
			Session session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
			MessageProducer producer = session.createProducer(exampleQueue);
			for (int i = 0; i < 50; i ++) {
				logger.info("Sending message #" + i);
				producer.send(session.createMessage());
			}
			session.close();
			conn.close();
			ctx.close();
			logger.info("done.");
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
		
	}	

}
